package com.che.libcommon.widget.viewpager;

import android.support.v4.view.ViewPager;
import android.view.View;

public class ScaleInPageTransformer implements ViewPager.PageTransformer {
    private float mMinScale = 0.86f;

    private static final float DEFAULT_CENTER = 0.5f;

    @Override
    public void transformPage(View view, float position) {
      int pageWidth = view.getWidth();
      int pageHeight = view.getHeight();
      view.setPivotX(pageWidth / 2);
      view.setPivotY(pageHeight / 2);
      if (position < -1) { // [-Infinity,-1)
        // This page is way off-screen to the left.
        view.setScaleX(mMinScale);
        view.setScaleY(mMinScale);
        view.setPivotX(pageWidth);

      } else if (position <= 1) { // [-1,1]
        // Modify the default slide transition to shrink the page as well
        if (position < 0) //1-2:1[0,-1] ;2-1:1[-1,0]
        {
          float scaleFactor = (1 + position) * (1 - mMinScale) + mMinScale;
          view.setScaleX(scaleFactor);
          view.setScaleY(scaleFactor);
          view.setPivotX(pageWidth * (DEFAULT_CENTER + (DEFAULT_CENTER * -position)));

        } else //1-2:2[1,0] ;2-1:2[0,1]
        {
          float scaleFactor = (1 - position) * (1 - mMinScale) + mMinScale;
          view.setScaleX(scaleFactor);
          view.setScaleY(scaleFactor);
          view.setPivotX(pageWidth * ((1 - position) * DEFAULT_CENTER));
        }

      } else { // (1,+Infinity]
        view.setScaleX(mMinScale);
        view.setScaleY(mMinScale);
        view.setPivotX(0);
      }

    }
  }